<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head th:replace="nav :: navHeaderIFrameItem(~{::title})">
    <title>号段新增功能</title>
</head>
<body>
    <div id="form_win">
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">业务类型</label>
                <div class="layui-input-block">
                    <input type="text" name="bizType" lay-verify="required" placeholder="业务类型" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">起始值</label>
                <div class="layui-input-block">
                    <input type="text" name="beginId" lay-verify="required|startValue" placeholder="起始值" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">步长</label>
                <div class="layui-input-block">
                    <input type="text" name="step" lay-verify="required" placeholder="步长" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否初始化</label>
                <div class="layui-input-block">
                    <input type="radio" name="isNeedInit" value="1" title="server">
                    <input type="radio" name="isNeedInit" value="0" title="sdk" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    <div th:include="nav::navFooterIFrameItem" ></div>
    <script>
        layui.use('form', function(){
            let form = layui.form,
                $ = layui.$;

            //表单验证
            form.verify({
                startValue: function(value){
                    if(value < 1){
                        return '初始值必须大于0';
                    }
                }
            });
            //监听提交
            form.on('submit(formDemo)', function(data){
                const index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
                //ajax请求的参数直接用data.field获取表单里含有name属性的元素的值
                //保存数据
                $.ajax({
                    type: "post",
                    url: "/segment/save",
                    data:JSON.stringify(data.field),
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    async:false,
                    success:function(result){
                        top.layer.close(index);
                        if(result.code === 0){
                            close_layer();
                            top.layer.msg("添加成功");
                        }else{
                            top.layer.msg(result.msg, {
                                icon: 2,
                                time: 3000 //如果不配置，默认是3秒
                            });
                        }
                    },
                    error : function(e){
                        top.layer.close(index);
                        top.layer.msg('服务异常');
                    }
                });
            });

            //关闭弹窗
            function close_layer() {
                let index = parent.layer.getFrameIndex(window.name);
                parent.window.frames["winCon"].layui.table.reload('tableData');
                parent.layer.close(index);
            }
        });
    </script>
</body>
</html>